package cn.taofuxn.sale.service.impl;

import cn.taofuxn.sale.bean.TbGoods;
import cn.taofuxn.sale.bean.TbProducer;
import cn.taofuxn.sale.controller.data.AddGoodsRequest;
import cn.taofuxn.sale.controller.data.ModifyGoodsRequest;
import cn.taofuxn.sale.dao.GoodsDao;
import cn.taofuxn.sale.dao.ProducerDao;
import cn.taofuxn.sale.service.GoodsService;
import cn.taofuxn.sale.service.data.base.BasePageResponse;
import cn.taofuxn.sale.service.data.base.BaseResult;
import cn.taofuxn.sale.service.data.base.ResultCodeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.Date;

/**
 * ProjectName : GeekSaleSystem
 * PackageName : cn.taofuxn.sale.service.impl
 * Created by Wong
 * Date: 2018/4/4
 * Time: 16:57
 */
@Service
public class GoodsServiceImpl implements GoodsService {
	
	@Autowired
	private GoodsDao m_GoodsDao;
	
	@Autowired
	private ProducerDao m_ProducerDao;
	
	@SuppressWarnings("Duplicates")
	@Override
	public BaseResult getGoods(final int vPage, final int vPageSize, final Date vStartDate, final Date vEndDate) {
		PageRequest tPageRequest = new PageRequest(vPage, vPageSize, new Sort("createTime"));
		Page tGoodsPage =  m_GoodsDao.findAll((vRoot, vCriteriaQuery, vCb) -> {
			Date tEnd = vEndDate;
			
			Predicate tPredicate = null;
			if(null != vStartDate && null == tEnd){
				tEnd = new Date(System.currentTimeMillis());
			}
			
			if(null != vStartDate)
				tPredicate = vCb.between(vRoot.get("createTime"), vStartDate, tEnd);
			
			return tPredicate;
		}, tPageRequest);
		
		BasePageResponse tResponse = new BasePageResponse();
		tResponse.setInfos(tGoodsPage.getContent());
		tResponse.setTotal((int) tGoodsPage.getTotalElements());
		tResponse.setTotalPage(tGoodsPage.getTotalPages());
		
		return BaseResult.success(tResponse);
	}
	
	@Override
	public BaseResult addGoods(final AddGoodsRequest vRequest) {
		TbProducer tTbProducer = m_ProducerDao.findOne(vRequest.getProducerId());
		if(null == tTbProducer){
			return BaseResult.generate(ResultCodeType.NOT_FOUND_PRODUCER);
		}
		
		TbGoods tTbGoods = new TbGoods();
		tTbGoods.setName(vRequest.getName());
		tTbGoods.setPlatform(vRequest.getPlatform());
		tTbGoods.setDiscountPrice(vRequest.getDiscountPrice());
		tTbGoods.setPrice(vRequest.getPrice());
		tTbGoods.setProvider(tTbProducer);
		return BaseResult.success(m_GoodsDao.save(tTbGoods));
	}
	
	@Override
	public BaseResult modifyGoods(final ModifyGoodsRequest vRequest) {
		TbGoods tTbGoods = m_GoodsDao.findOne(vRequest.getId());
		if(null == tTbGoods){
			return BaseResult.generate(ResultCodeType.GOODS_NOT_FOUND);
		}
		
		String tProducerId = vRequest.getProducerId();
		if(!tTbGoods.getProvider().getId().equals(tProducerId)) {
			TbProducer tTbProducer = m_ProducerDao.findOne(tProducerId);
			if(null == tTbProducer){
				return BaseResult.generate(ResultCodeType.NOT_FOUND_PRODUCER);
			}
			
			tTbGoods.setProvider(tTbProducer);
		}
		
		tTbGoods.setName(vRequest.getName());
		tTbGoods.setPrice(vRequest.getPrice());
		tTbGoods.setPlatform(vRequest.getPlatform());
		tTbGoods.setProviderPrice(vRequest.getProviderPrice());
		tTbGoods.setDiscountPrice(vRequest.getDiscountPrice());
		return BaseResult.success(m_GoodsDao.save(tTbGoods));
	}
	
	@Override
	public BaseResult deleteGoods(final String vGoodsId) {
		//要先删除关于商品的订单？
		m_GoodsDao.delete(vGoodsId);
		return BaseResult.success();
	}
}
